//
//  HJFirendVerifyController.m
//  KaiChat
//
//  Created by lx on 2024/3/26.
//

#import "HJFirendVerifyController.h"
#import "QMUIButton.h"
#import "HJIM.h"
#import "HJAttributedStringFactory.h"

@interface HJFirendVerifyController ()

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIView *maskView;
@property (nonatomic, weak) QMUIButton *dismissButton;
@property (nonatomic, strong) NSArray *datas;


@end

@implementation HJFirendVerifyController

- (void)showInController:(UIViewController *)controller {
    [controller presentViewController:self animated:NO completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = UIColor.clearColor;
    
    UIButton *maskView = [[UIButton alloc] initWithFrame:self.view.bounds].addTo(self.view);
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [maskView addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    _maskView = maskView;
    
    
    NSString *signatureText = nil;
    
    if (isEmptyString(self.userModel.brief)) {
        signatureText = @"what's up:";
    } else {
        signatureText = self.userModel.brief;
    }
    
    HJAttributedStringFactory *attributedStringFactory = [[HJAttributedStringFactory alloc] initWithString:signatureText maxWidth:KScreenWidth - leftRightMargin * 2 - 15 * 2 font:UIFontMake(12) lineSpacing:3 alignment:NSTextAlignmentLeft];
    CGSize signatureSize = attributedStringFactory.size;
    
    CGFloat contentViewH = TabbarSafeBottomMargin + 70 + signatureSize.height + HZoom(64) + 86 + 20;
    
    UIView *contentView = [UIView addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    
    _contentView = contentView;
    
    UIView *titleView = [UIView addToView:contentView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.right.offset(0);
        make.height.offset(50);
    }];
    
    QMUIButton *closeButton = [QMUIButton addToView:titleView];
    [closeButton addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    [closeButton setImage:[UIImage imageNamed:@"nav_close"] forState:UIControlStateNormal];
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.width.height.offset(30);
        make.left.offset(15);
    }];
    
    UIView *infoView = [UIView addToView:contentView];
    infoView.backgroundColor = UIColor.whiteColor;
    infoView.addCornerRadius(10);
    infoView.layer.borderColor = UIColor.tj_separatorColor.CGColor;
    infoView.layer.borderWidth = 0.5;
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(titleView.mas_bottom);
    }];
    
    UIImageView *topBackgroundImageView = [UIImageView addToView:infoView];
    topBackgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    topBackgroundImageView.clipsToBounds = YES;
    topBackgroundImageView.image = [UIImage imageWithContentsOfName:@"userHomePage_top_bg" type:@"png"];
    [topBackgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.right.offset(0);
        make.height.offset(HZoom(64));
    }];
    
    UIImageView *avatarImageView = [UIImageView addToView:infoView];
    avatarImageView.addCornerRadius(34);
    avatarImageView.layer.borderColor = UIColor.whiteColor.CGColor;
    avatarImageView.layer.borderWidth = 1;
    avatarImageView.image = [UIImage imageNamed:defaultAvatarName];
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.width.height.offset(68);
        make.centerY.equalTo(topBackgroundImageView.mas_bottom);
    }];
    
    UILabel *nicknameLabel = [UILabel lableWithSuperView:infoView font:UIFontMediumMake(18) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatarImageView.mas_bottom).offset(12);
        make.left.offset(15);
        make.right.mas_lessThanOrEqualTo(-leftRightMargin);
    }];
    
    UILabel *signatureLabel = [UILabel lableWithSuperView:infoView font:UIFontMake(12) color:UIColor.tj_868391Color title:nil textAlignment:NSTextAlignmentLeft];
    signatureLabel.numberOfLines = 0;
    [signatureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nicknameLabel.mas_bottom).offset(4);
        make.left.right.inset(15);
    }];
    
    HJStyle1Button *applyAddButton = [HJStyle1Button buttonWithSuperView:contentView font:UIFontMediumMake(14) color:nil title:@"Send friend requests..."];
    [applyAddButton addTarget:self action:@selector(didClickApplyAddButton) forControlEvents:UIControlEventTouchUpInside];
    [applyAddButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.height.offset(buttonHeight);
        make.bottom.inset(TabbarSafeBottomMargin + 10);
    }];
    
    if (self.userModel.relationship == -1 || self.userModel.relationship == 3) {
        
     
    } else if (self.userModel.relationship == 2) {
        if (self.userModel.way == 1) {//自己发起的
            [applyAddButton setTitle:@"Waiting" forState:UIControlStateNormal];
            applyAddButton.enabled = NO;
            [applyAddButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        } else {
            [applyAddButton setTitle:@"Send friend requests..." forState:UIControlStateNormal];
            [applyAddButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            applyAddButton.enabled = YES;
        }
    }
    
    [[SDWebImageManager sharedManager] loadImageWithURL:self.userModel.avatar.URL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        if (image) {
            avatarImageView.image = image;
        }
    }];
    
    signatureLabel.attributedText = attributedStringFactory.attributedString;
    nicknameLabel.text = self.userModel.nickname;
    
    if (isEmptyString(self.userModel.walletAddress)) {
        [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(signatureLabel.mas_bottom).offset(15);
        }];
    } else {
        
        UIView *walletAddressView = [UIView addToView:infoView];
        walletAddressView.backgroundColor = UIColor.tj_F7F8FAColor;
        walletAddressView.addCornerRadius(5);
        UILabel *addressLabel = [UILabel lableWithSuperView:walletAddressView font:UIFontMake(12) color:UIColor.tj_868391Color title:nil textAlignment:NSTextAlignmentLeft];
        addressLabel.numberOfLines = 0;
        [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.inset(6);
            make.left.right.inset(15);
        }];
        
        [walletAddressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(signatureLabel);
            make.top.equalTo(signatureLabel.mas_bottom).offset(12);
            make.bottom.equalTo(addressLabel).offset(6);
        }];
        
        [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(walletAddressView.mas_bottom).offset(15);
        }];
        
        contentViewH += 88;
        addressLabel.text = self.userModel.walletAddress;
    }
    
    contentViewH = MAX(350, contentViewH);
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, contentViewH);
    [contentView addRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:20];
}


- (void)didClickApplyAddButton {
    if (_userModel.relationship == -1 || _userModel.relationship == 3 || (_userModel.relationship == 2 && _userModel.way == 2)) {
        [HJHUD showGraceTimeLoadingInView:self.view];
        [HJIM.shareIM.userManager sendApplyAddFriendWithUserId:self.userId complete:^(NSError * _Nullable error) {
            if (!error) {
                [self dismissComplete:nil];
            }
        }];
    }
}

- (void)didClickDismissButton {
    [self dismissComplete:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
        self.maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
    }];
    
}

- (void)dismissComplete:(void (^)(void))complete {
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) {
                complete();
            }
        }];
    }];
}

- (void)loadData {
    
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationCustom;
}

- (void)dealloc {
    TJLog(@"%s", __func__);
}

@end





